﻿Imports System.Net
Imports TNCBAL
Imports TNCDAL
Imports HTLibrary

Public Class ucVessel
    Dim db As New TanNamChinhDataContext
    Private m_vessel As New CVessel

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        DataGridViewHelper.DataGridView_InitEvent(dtgVessel)
    End Sub

    Private Sub ucVessel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        KhoiTao_GiaoDien(True)
        KhoiTaodtgVessel()
    End Sub

    Public Sub KhoiTaodtgVessel(Optional ByVal NameUpdate As String = "", Optional ByVal Name As String = "")
        Dim fc = m_vessel.SelectAllRecordByName(Name)

        Dim source As New BindingSource
        source.DataSource = fc
        BindingNavigator1.BindingSource = source

        dtgVessel.DataSource = source
        FormatdtgVessel()

        Try
            If NameUpdate = "" Then
                dtgVessel.Rows(0).Selected = True
                dtgVessel.FirstDisplayedScrollingRowIndex = 0
            Else
                Dim index As Integer = 0
                For i As Integer = 0 To dtgVessel.RowCount - 1
                    If dtgVessel.Rows(i).Cells("VesselName").Value = NameUpdate Then
                        index = i
                        Exit For
                    End If
                Next

                dtgVessel.Rows(index).Cells("VesselName").Selected = True
            End If
        Catch ex As Exception

        End Try

        'dtgVessel.Rows(indexRow).Selected = True
        'dtgVessel.FirstDisplayedScrollingRowIndex = indexRow
    End Sub

    Public Sub FormatdtgVessel()
        'dtgVessel.RowHeadersWidth = 20
        dtgVessel.RowHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgVessel.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

        dtgVessel.MultiSelect = False
        dtgVessel.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgVessel.ReadOnly = True
        dtgVessel.BackgroundColor = Color.White
        dtgVessel.AllowUserToAddRows = False
        dtgVessel.AllowUserToDeleteRows = False

        dtgVessel.Columns("ID_Vessel").Visible = False
        dtgVessel.Columns("IsActive").Visible = False
        With dtgVessel.Columns("VesselName")
            .HeaderText = "Vessel"
        End With
    End Sub

#Region "btn Them, Xoa, Sua"
    Dim f_Them As Boolean = False
    Dim f_Sua As Boolean = False

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Clear()
        txtVessel.Focus()
        KhoiTao_GiaoDien(False)
        f_Them = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        KhoiTao_GiaoDien(False)
        txtVessel.Focus()
        f_Sua = True
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        Dim ID_Vessel As Integer = dtgVessel.CurrentRow.Cells("ID_Vessel").Value
        'kiem tra truoc khi xoa
        Dim FCName As String = dtgVessel.CurrentRow.Cells("VesselName").Value
        If (MsgBox("Bạn muốn xóa " + FCName + " ra khỏi danh sách", MsgBoxStyle.YesNo, "Chú ý") = MsgBoxResult.Yes) Then
            Try
                m_vessel.Delete(ID_Vessel)
                KhoiTaodtgVessel(0)
                'write systemlog
                Dim objLogController As New LogController
                objLogController.Insert(enumAction.Delete, String.Format("Vessel Name {0}", FCName))
            Catch ex As Exception
                MsgBox("Del - Vessel: " + FCName + " đang được sử dụng. Không xóa được")
            End Try
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim obj_vessel As New tblVessel
        obj_vessel.VesselName = txtVessel.Text

        If f_Them Then
            obj_vessel.IsActive = True
            m_vessel.Insert(obj_vessel)
            KhoiTaodtgVessel(txtVessel.Text, "")
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Add, String.Format("Vessel Name {0}", obj_vessel.VesselName))
        End If

        If f_Sua Then
            obj_vessel.ID_Vessel = dtgVessel.CurrentRow.Cells("ID_Vessel").Value
            Dim index As Integer = dtgVessel.CurrentRow.Index
            m_vessel.Update(obj_vessel)
            KhoiTaodtgVessel(txtVessel.Text, "")
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Edit, String.Format("Vessel Name {0}", obj_vessel.VesselName))
        End If

        f_Them = False
        f_Sua = False
        KhoiTao_GiaoDien(True)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        KhoiTao_GiaoDien(True)
        KhoiTaodtgVessel(dtgVessel.CurrentRow.Cells("VesselName").Value, txtNameSearch.Text)
    End Sub

    Public Sub KhoiTao_GiaoDien(ByVal flag As Boolean)
        btnAdd.Enabled = flag
        btnEdit.Enabled = flag
        btnDel.Enabled = flag
        btnUpdate.Enabled = Not flag
        btnCancel.Enabled = Not flag
        txtVessel.ReadOnly = flag

        dtgVessel.Enabled = flag

        'If flag Then
        '    txtVessel.BackColor = Color.Azure
        'Else
        '    txtVessel.BackColor = Color.White
        'End If
    End Sub

    Public Sub Clear()
        txtVessel.Text = ""
    End Sub
#End Region

    Private Sub dtgVessel_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgVessel.CurrentCellChanged
        Try
            txtVessel.Text = dtgVessel.CurrentRow.Cells("VesselName").Value
        Catch ex As Exception

        End Try

    End Sub
    
    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        KhoiTaodtgVessel("", txtNameSearch.Text.Trim)
    End Sub

    Private Sub btnCancelSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelSearch.Click
        txtNameSearch.Text = ""
        KhoiTaodtgVessel("", txtNameSearch.Text.Trim)
    End Sub

    Private Sub txtNameSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNameSearch.KeyDown
        If e.KeyCode = Keys.Enter Then
            KhoiTaodtgVessel("", txtNameSearch.Text.Trim)
        End If
    End Sub
End Class
